package com.bukkit.feverdream.noon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class Settings {

    private String _configFile = "noon.properties";
    private String _configComment = "Noon Control Configuration File.";
    private String _configValueMode = "always-time";
    private String _configValueModeDefault = "off";
    private String _mode = "off";
    private Noon _noon = null;
    
    public Settings(Noon plugin){
        _noon = plugin;
    }
    public void saveDefaultSettings() {
        Properties props = new Properties();

        props.setProperty(_configValueMode, _configValueModeDefault);
        
        try {
            OutputStream propOut = new FileOutputStream(new File(_configFile));
            props.store(propOut, _configComment);
        } catch (IOException ioe) {
            _noon.getLog().warning("[Noon] " + ioe.getMessage());
        }
    }

    public void saveSettings() {
        Properties props = new Properties();

        props.setProperty(_configValueMode, _mode);

        try {
            OutputStream propOut = new FileOutputStream(new File(_configFile));
            props.store(propOut, _configComment);
        } catch (IOException ioe) {
            _noon.getLog().warning("[Noon] " + ioe.getMessage());        }
    }

    public Boolean loadSettings() {
        Boolean result = false;

        Properties props = new Properties();
        try {
            props.load(new FileInputStream(_configFile));

            String mode = props.getProperty(_configValueMode, _configValueModeDefault).trim().toLowerCase();
            result = setMode(mode);
        } catch (IOException ioe) {
            props.clear();
            _noon.getLog().warning("[Noon] Load Properties failed. Resetting to defaults");
            this.saveDefaultSettings();
        }
        return result;
    }

    public boolean setMode(String mode) {
        boolean result = false;
        if (mode.equalsIgnoreCase("day")
                || mode.equalsIgnoreCase("sunset")
                || mode.equalsIgnoreCase("night")
                || mode.equalsIgnoreCase("sunrise")
                || mode.equalsIgnoreCase("off")) {
            _mode = mode;
            result = true;

        }
        return result;
    }

    public String getMode() {
        return _mode;
    }
}
